package com.ibuonline.b2b.common;

import java.io.IOException;
import java.util.Random;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;

import com.dimeng.common.util.SystemConfig;

import net.spy.memcached.AddrUtil;
import net.spy.memcached.MemcachedClient;

public class SpyMemcached implements CacheClient {
	
	private MemcachedClient[] clients = null;
	private static final String CACHE_SERVER = SystemConfig.getProperty("hibernate.memcached.url",	
			"192.168.0.242:11211 192.168.0.243:11211");
	private static SpyMemcached instance = new SpyMemcached();     
	private static final int DEFAULT_EXPIRE = 8 * 3600;
	private static final int MAX_CLIENTS = 10;
	private static final Random rnd = new Random(System.currentTimeMillis());
	
	public static synchronized SpyMemcached getInstance() {
		return instance;
	}
	
	private SpyMemcached() {
		System.out.println("Initializing memcached client......");
		clients = new MemcachedClient[MAX_CLIENTS];
		try {
			for (int i = 0; i < MAX_CLIENTS; i ++)
				clients[i] = new MemcachedClient(AddrUtil.getAddresses(CACHE_SERVER));
		} catch (IOException e) {
			e.printStackTrace();
		}
		System.out.println("Initializing completed.");
	}

	public void delete(String key) {
		getClient().delete(key);
	}

	public Object get(String key) {
		return getClient().get(key);
	}
	
	public Object asyncGet(String key) {
		Object value = null;
		Future<Object> future = getClient().asyncGet(key); 
		try {     
			value = future.get(5, TimeUnit.SECONDS);
		} catch(TimeoutException e) {     
			future.cancel(false);
			e.printStackTrace();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return value;
	}
	
	public void replace(String key, int expire, Object value) {
		getClient().replace(key, expire, value);
	}
	
	public void touch(String key, int expire) {
		getClient().touch(key, expire);
	}
	
	public void replace(String key, Object value) {
		getClient().replace(key, DEFAULT_EXPIRE, value);
	}

	public void set(String key, int expire, Object value) {
		getClient().set(key, expire, value);
	}

	public void set(String key, Object value) {
		getClient().set(key, DEFAULT_EXPIRE, value);
	}

	public MemcachedClient getClient() {
		return clients[rnd.nextInt(MAX_CLIENTS)];
	}
	
	@SuppressWarnings("unchecked")
	public static void main(String[] args) throws Exception {
		java.util.List<Integer> roleList = new java.util.ArrayList<Integer>(3);
		roleList.add(102);
		roleList.add(103);
		roleList.add(104);
		SpyMemcached.getInstance().set("dimeng", 3, roleList);
		System.out.println(SpyMemcached.getInstance().get("dimeng"));
		SpyMemcached.getInstance().set("dimeng", 3, "shangjiwang");
		//Thread.sleep(2000);
		//java.util.List<Integer> list = (java.util.List<Integer>)SpyMemcached.getInstance().get("dimeng");
		//int i = 0;
		//for (i = 0; (java.util.List<Integer>)SpyMemcached.getInstance().get("dimeng") != null; i++) {
		//}
		//System.out.println(i);
		System.out.println(SpyMemcached.getInstance().get("dimeng"));
		//Thread.sleep(700);
		//java.util.List<Integer> list2 = (java.util.List<Integer>)SpyMemcached.getInstance().get("dimeng");
		//System.out.println(list);
		//System.out.println(list2);
	}
}
